package com.ericcson.innerportalapp;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PeopleDetailArrayAdapter extends ArrayAdapter<PeopleListItem> {

	int resource;
	Context context;
	PeopleListItem peopleObj = null;
	
	public PeopleDetailArrayAdapter(Context context, int resource,
			PeopleListItem[] list) {
		super(context, resource, list);
		this.resource = resource; 
		this.context = context;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		peopleObj = getItem(position);
		LayoutInflater layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View listViewItem = layoutInflator.inflate(resource, null);
		
		final TextView textView = (TextView) listViewItem.findViewById(R.id.detailTw);
		final TextView textView1 = (TextView) listViewItem.findViewById(R.id.detailTw1);
		//final TextView textView2 = (TextView) listViewItem.findViewById(R.id.detailTw2);
		
		textView1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(textView.getText().toString().toLowerCase().equals("work")){
					Intent callIntent = new Intent(Intent.ACTION_CALL);
			        callIntent.setData(Uri.parse("tel:"+peopleObj.getWorkPhone()));
			        context.startActivity(callIntent);
				}
				else if(textView.getText().toString().toLowerCase().equals("mobile")){
					Intent sms=new Intent();
					sms.setAction(android.content.Intent.ACTION_VIEW);
					sms.setData(Uri.parse("smsto:"+peopleObj.getMobilePhone().toString()));
					context.startActivity(sms);

				}
				else if(textView.getText().toString().toLowerCase().equals("email")){
					Intent emailIntent = new Intent(Intent.ACTION_SENDTO, 
					Uri.fromParts("mailto",peopleObj.getEmail(), null));
					context.startActivity(Intent.createChooser(emailIntent, "Send email..."));			        
				}
			}
		});
	
		switch (position) {
		case 0:
			textView.setText("Title");
			String title = peopleObj.getTitle();
			textView1.setText(title);
			break;
		case 1:
			textView.setText("Department");
			String dep = peopleObj.getDepart();
			textView1.setText(dep);
			break;	
		case 2:
			textView.setText("Work");
			String wp = peopleObj.getWorkPhone();
			textView1.setText(wp);
			break;
		case 3:
			textView.setText("Mobile");
			String mp = peopleObj.getMobilePhone();
			textView1.setText(mp);
			break;
		case 4:
			textView.setText("Email");
			String email = peopleObj.getEmail();
			textView1.setText(email);
			break;
		case 5:
			textView.setText("Manager");
			String man = peopleObj.getManager();
			textView1.setText(man);
			break;
		}
		
		return listViewItem;
	}

}
